package com.vvlhw.pptmall.product.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vvlhw.common.utils.PageUtils;
import com.vvlhw.common.utils.Query;

import com.vvlhw.pptmall.product.dao.GoodsDao;
import com.vvlhw.pptmall.product.entity.GoodsEntity;
import com.vvlhw.pptmall.product.service.GoodsService;


@Service("goodsService")
public class GoodsServiceImpl extends ServiceImpl<GoodsDao, GoodsEntity> implements GoodsService {

    @Autowired
    GoodsDao goodsDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<GoodsEntity> page = this.page(
                new Query<GoodsEntity>().getPage(params),
                new QueryWrapper<GoodsEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 全字段、分类的模糊查询
     * @param catId 注意可能null 或者 空串
     * @param other 同上
     * @return
     */
    @Override
    public List<GoodsEntity> getBycatIdAndOther(Long catId, String other) {
//        GoodsEntity good = new GoodsEntity();
//        if (catId != null) {
//            good.setCatId(catId);
//        }
        // 有other的情况
        // catId有值。eq这里就生效。利用eq一个重载函数
        // catId无值。eq这里不生效
        if (other != null && !other.isEmpty()) {
            // 这种lambda更好检查，不需要手敲数据库表字段，注释那种敲错了还不报错！！！
            return new LambdaQueryChainWrapper<>(this.baseMapper)
                    .eq(catId != null, GoodsEntity::getCatId, catId)
                    .and(warpper -> warpper.like(GoodsEntity::getGid, other)
                            .or().like(GoodsEntity::getGoodName, other)
                            .or().like(GoodsEntity::getDescription, other)
                            .or().like(GoodsEntity::getAuthor, other)
                            .or().like(GoodsEntity::getCatName, other)
                            .or().like(GoodsEntity::getPrice, other)
                            .or().like(GoodsEntity::getSale, other))
                    .list();
//            QueryWrapper<GoodsEntity> queryWrapper = new QueryWrapper<>();
//            queryWrapper.eq(catId != null,"cat_id", catId).and(wrapper -> wrapper.like("gid", other)
//                    .or().like("good_name", other)
//                    .or().like("description", other)
//                    .or().like("author", other)
//                    .or().like("cat_name", other)
//                    .or().like("price", other)
//                    .or().like("sale", other));
//            return this.baseMapper.selectList(queryWrapper);
        } else {
            // 没有other的情况
            // catId有值：查该分类的所有。上面第一个if自然就set进去，根据MP的动态就能查到
            // catId无值：查询所有。同上，没set就不会加分类条件
            return new LambdaQueryChainWrapper<>(this.baseMapper).eq(catId != null, GoodsEntity::getCatId, catId).list();
//            QueryWrapper<GoodsEntity> wrapper = new QueryWrapper<>(good);
//            return this.baseMapper.selectList(wrapper);
        }
    }

    @Override
    public void updateCatNameByCatId(Long catId, String catName) {
        // 其实我用到了上面的getBycatIdAndOther。
        // 先查询出所有catId 然后改其catName就好了
        List<GoodsEntity> list = getBycatIdAndOther(catId, null);
        list.forEach(goodsEntity -> goodsEntity.setCatName(catName));
        this.updateBatchById(list);
//        System.out.println("updateCatNameByCatId 修改成功");
    }

    @Override
    public Long mySave(GoodsEntity goods) {
        // 调用dao的，不是service的
        goodsDao.mySave(goods);
        // 新增id已经存进去，返回就好
        return goods.getGid();
    }

}